package com.example.examsystem2.controller;

import com.example.examsystem2.entity.*;
import com.example.examsystem2.entity.common.ResponseResult;
import com.example.examsystem2.service.*;
import com.example.examsystem2.util.JsonUtils;
import com.example.examsystem2.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("exam_system")
public class ExamPaperController {

    @Autowired
    private ExamPaperService examPaperService;
    ResponseResult responseResult=new ResponseResult();

    @Autowired
    private SingleChoiceQService singleChoiceQService;
    @Autowired
    private JudgmentQService judgmentQService;

    @Autowired
    private ShortAnswerQService shortAnswerQService;

    @Autowired
    private FillBlanksQService fillBlanksQService;


    @RequestMapping("/exam_paper")
    public String exam_paper(){
        return "views/manage/exam_paper";
    }

    @RequestMapping("/add_exam_paper")
    @ResponseBody
    public String add_exam_paper(ExamPaper examPaper){
        examPaper.setEp_creat_time(TimeUtils.getTime());
        int i = examPaperService.add_exam_paper(examPaper);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("添加成功!立即查看？");
            responseResult.setResult(examPaper.getEp_id());
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("添加失败");
        }
        return JsonUtils.getJson(responseResult);
    }



    @RequestMapping("/exam_paper_library")
    public String exam_paper_library(){
        return "views/manage/exam_paper_library";
    }


    @RequestMapping("/get_all_exam_paper")
    @ResponseBody
    public String get_all_exam_paper(ExamPaper examPaper){
        List<ExamPaper> all_exam_paper = examPaperService.get_all_exam_paper(examPaper);
        responseResult.setCode(0);
        responseResult.setMessage("成功");
        responseResult.setResult(all_exam_paper);
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/del_exam_paper_by_ep_id")
    @ResponseBody
    public String del_exam_paper_by_ep_id(int ep_id){
        int i = examPaperService.del_exam_paper_by_ep_id(ep_id);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("删除成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("删除失败");
        }
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/get_exam_paper")
    public String get_exam_paper(int ep_id, Model model){
        model.addAttribute("ep_id", ep_id);
        return "views/manage/browse_exam_paper";
    }

    @RequestMapping("/get_exam_paper_by_ep_id")
    @ResponseBody
    public String get_exam_paper_by_ep_id(int ep_id){
        ExamPaper examPaperByEpId = examPaperService.get_exam_paper_by_ep_id(ep_id);

        if(examPaperByEpId == null){
            responseResult.setCode(500);
            responseResult.setMessage("");
            return JsonUtils.getJson(responseResult);
        }

        Map<String,Object> map=new HashMap<>();
        map.put("paper_res",examPaperByEpId);
        String ep_scq_c_ids = examPaperByEpId.getEp_scq_c_ids();
        String[] scq_c_ids = ep_scq_c_ids.split(",");
        List<Object> scq_data = new ArrayList<>();
        for (String c_id : scq_c_ids) {
            if (!c_id.equals("")) {
                SingleChoiceQuestions scqByCId = singleChoiceQService.get_scq_by_c_id(Integer.parseInt(c_id));
                if (scqByCId==null){
                    continue;
                }
                scq_data.add(scqByCId);

            }
        }
        map.put("scq_data", scq_data);

        String ep_jq_j_ids = examPaperByEpId.getEp_jq_j_ids();
        String[] jq_j_ids = ep_jq_j_ids.split(",");
        List<Object> jq_data = new ArrayList<>();
        for (String j_id : jq_j_ids) {
            if (!j_id.equals("")) {
                JudgmentQuestions jqByJId = judgmentQService.get_jq_by_j_id(Integer.parseInt(j_id));
                if (jqByJId==null){
                    continue;
                }
                jq_data.add(jqByJId);
            }
        }
        map.put("jq_data", jq_data);

        String ep_saq_sa_ids = examPaperByEpId.getEp_saq_sa_ids();
        String[] saq_sa_ids = ep_saq_sa_ids.split(",");
        List<Object> saq_data = new ArrayList<>();
        for (String sa_id : saq_sa_ids) {
            if (!sa_id.equals("")) {
                ShortAnswerQuestions saqBySaId = shortAnswerQService.get_saq_by_sa_id(Integer.parseInt(sa_id));
                if (saqBySaId==null){
                    continue;
                }
                saq_data.add(saqBySaId);
            }
        }
        map.put("saq_data", saq_data);

        String ep_fbq_fb_ids = examPaperByEpId.getEp_fbq_fb_ids();
        String[] fbq_fb_ids = ep_fbq_fb_ids.split(",");
        List<Object> fbq_data = new ArrayList<>();
        for(String fb_id:fbq_fb_ids){
            if (!fb_id.equals("")) {
                FillBlanksQuestions fbqByFbId = fillBlanksQService.get_fbq_by_fb_id(Integer.parseInt(fb_id));
                if (fbqByFbId==null){
                    continue;
                }
                fbq_data.add(fbqByFbId);
            }
        }
        map.put("fbq_data", fbq_data);

        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setResult(map);
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/get_exam_paper_by_s_id_and_level")
    @ResponseBody
    public String get_exam_paper_by_s_id_and_level(ExamPaper examPaper){
        List<ExamPaper> examPaperBySIdAndLevel = examPaperService.get_exam_paper_by_s_id_and_level(examPaper);
        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setResult(examPaperBySIdAndLevel);
        return JsonUtils.getJson(responseResult);
    }

}
